package cn.studyjava.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.studyjava.mybatis.mapper.UserMapper;
import cn.studyjava.mybatis.model.User;
import cn.studyjava.mybatis.model.UserExample;
import cn.studyjava.mybatis.model.UserExample.Criteria;
import cn.studyjava.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public User getUser(String name) throws Exception {
		UserExample userExample = new UserExample();
		Criteria userCriteria = userExample.createCriteria();
		userCriteria.andNameEqualTo(name);
		List<User> users = userMapper.selectByExample(userExample);
		if(users.size() == 1){
			return users.get(0);
		}else if(users.size() > 1){
			throw new Exception("用户名重复，请联系管理员");
		}
		return null;
	}

}
